from PyQt6.QtWidgets import (
    QWidget, QVBoxLayout, QPushButton, QLabel, QGroupBox,
    QProgressBar, QApplication
)
from PyQt6.QtGui import QFont
from PyQt6.QtCore import Qt, QTimer, QThread

class AdvancedTab:
    """高级功能标签页"""
    
    def __init__(self, status_bar=None):
        self.status_bar = status_bar
        self.timer_value = 0
        self.timer_running = False
        self.count_timer = QTimer()
        self.count_timer.timeout.connect(self.update_timer)
        
    def create_tab(self):
        """创建高级功能标签页"""
        tab = QWidget()
        layout = QVBoxLayout(tab)
        
        title = QLabel("高级功能")
        title.setFont(QFont("Arial", 16, QFont.Weight.Bold))
        title.setAlignment(Qt.AlignmentFlag.AlignCenter)
        layout.addWidget(title)
        
        # 样式表示例
        style_group = QGroupBox("样式表 (CSS样式)")
        style_layout = QVBoxLayout(style_group)
        
        styled_btn = QPushButton("样式化按钮")
        styled_btn.setStyleSheet("""
            QPushButton {
                background-color: #3498db;
                color: white;
                border-radius: 10px;
                padding: 10px;
                font-weight: bold;
            }
            QPushButton:hover {
                background-color: #2980b9;
            }
            QPushButton:pressed {
                background-color: #1c6ea4;
            }
        """)
        
        styled_label = QLabel("样式化标签")
        styled_label.setStyleSheet("""
            QLabel {
                background-color: #2ecc71;
                color: white;
                padding: 10px;
                border: 2px solid #27ae60;
                border-radius: 5px;
                font-style: italic;
            }
        """)
        
        style_layout.addWidget(styled_btn)
        style_layout.addWidget(styled_label)
        layout.addWidget(style_group)
        
        # 定时器示例
        timer_group = QGroupBox("定时器")
        timer_layout = QVBoxLayout(timer_group)
        
        self.timer_label = QLabel("00:00")
        self.timer_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.timer_label.setFont(QFont("Arial", 24, QFont.Weight.Bold))
        
        timer_btn = QPushButton("开始/停止定时器")
        timer_btn.clicked.connect(self.toggle_timer)
        
        reset_btn = QPushButton("重置定时器")
        reset_btn.clicked.connect(self.reset_timer)
        
        timer_layout.addWidget(self.timer_label)
        timer_layout.addWidget(timer_btn)
        timer_layout.addWidget(reset_btn)
        layout.addWidget(timer_group)
        
        # 多线程示例
        thread_group = QGroupBox("多线程")
        thread_layout = QVBoxLayout(thread_group)
        
        thread_info = QLabel("在实际应用中，长时间任务应在单独线程中运行\n以避免阻塞UI线程")
        thread_info.setWordWrap(True)
        
        self.progress_bar = QProgressBar()
        self.progress_bar.setRange(0, 100)
        
        thread_btn = QPushButton("模拟耗时任务")
        thread_btn.clicked.connect(self.simulate_long_task)
        
        thread_layout.addWidget(thread_info)
        thread_layout.addWidget(self.progress_bar)
        thread_layout.addWidget(thread_btn)
        layout.addWidget(thread_group)
        
        return tab
    
    def toggle_timer(self):
        """切换定时器状态"""
        self.timer_running = not self.timer_running
        if self.timer_running:
            self.count_timer.start(100)  # 每100毫秒更新一次
        else:
            self.count_timer.stop()
    
    def reset_timer(self):
        """重置定时器"""
        self.timer_value = 0
        self.update_timer_display()
    
    def update_timer(self):
        """更新定时器"""
        self.timer_value += 1
        self.update_timer_display()
    
    def update_timer_display(self):
        """更新定时器显示"""
        seconds = self.timer_value / 10
        self.timer_label.setText(f"{seconds:.1f} 秒")
    
    def simulate_long_task(self):
        """模拟耗时任务"""
        self.progress_bar.setValue(0)
        if self.status_bar:
            self.status_bar.showMessage("模拟耗时任务开始...")
        
        # 在实际应用中，这里应该使用QThread
        for i in range(1, 101):
            self.progress_bar.setValue(i)
            QApplication.processEvents()  # 允许UI更新
            QThread.msleep(30)  # 模拟工作延迟
        
        if self.status_bar:
            self.status_bar.showMessage("耗时任务完成!", 3000)